package lk.uom.cse.sac.business;

import lk.uom.cse.sac.dao.UserDAO;
import lk.uom.cse.sac.exception.UserManagerException;
import lk.uom.cse.sac.model.User;

public class UserManager {
	
	private UserDAO userDao;
	
	UserManager(){
		userDao = new UserDAO();
	}
	
	public User getUser(Long userId){
		return userDao.getUser(userId);		
	}
	
	public User saveUser(User user) throws UserManagerException{
		User savedUser = userDao.getUser(user.getUserName());
		if(savedUser != null){
			throw  new UserManagerException();
		}
		return userDao.saveUser(user);		
	}
	
	public void deleteUser(User user){
		User savedUser = userDao.getUser(user.getUserName());
		if(savedUser != null){
			userDao.deleteUser(user);
		}
	}
	
	public void deleteUser(Long userId){
		User user = this.getUser(userId); 
		this.deleteUser(user);		
	}
	
	public User updateUser(User user){
		return userDao.updateUser(user);		
	}

	public void deleteAll() {
		userDao.deleteAll();	
	}

}
